package com.ttg.controller;

import com.ttg.config.Properties;
import com.ttg.contants.KeyContants;
import com.ttg.data.InitGatewayReqData;
import com.ttg.pojo.AppPayReqDTO;
import com.ttg.security.SecurityKit;
import com.ttg.utils.HttpKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping
public class GwAppPayDemoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GwPayDemoController.class);

    @RequestMapping("/app_pay")
    public ModelAndView testPayDemo() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("demo/jspay_req_demo");

        AppPayReqDTO appPayReq = InitGatewayReqData.initAppPayReq();
        String        url        = "req_jspay";

        LOGGER.debug("请求地址：{}", url);
        modelAndView.addObject("url", url);
        modelAndView.addObject("payment", appPayReq);

        return modelAndView;
    }


    @RequestMapping(
            value    = "/req_jspay",
            method   = RequestMethod.POST
    )
    public ModelAndView sendPayReq(@RequestParam(
            value    = "merchantId",
            required = true
    ) String merchantId, @RequestParam(
            value    = "merOrderId",
            required = true
    ) String merOrderId, @RequestParam(
            value    = "feeAmt",
            required = true
    ) Integer feeAmt, @RequestParam(
            value    = "currency",
            required = true
    ) String currency, @RequestParam(
            value    = "subject",
            required = true
    ) String subject, @RequestParam(
            value    = "detail",
            required = false
    ) String detail, @RequestParam(
            value    = "transAmt",
            required = true
    ) Integer transAmt, @RequestParam(
            value    = "bgRetUrl",
            required = false
    ) String bgRetUrl, @RequestParam(
            value    = "pgRetUrl",
            required = false
    ) String pgRetUrl, @RequestParam(
            value    = "attach",
            required = false
    ) String attach) {
        AppPayReqDTO appPayReqDTO = new AppPayReqDTO();

        appPayReqDTO.setMerchantId(merchantId);
        appPayReqDTO.setMerOrderId(merOrderId);
        appPayReqDTO.setFeeAmt(feeAmt);
        appPayReqDTO.setCurrency(currency);
        appPayReqDTO.setSubject(subject);
        appPayReqDTO.setDetail(detail);
        appPayReqDTO.setTransAmt(transAmt);
        appPayReqDTO.setBgRetUrl(bgRetUrl);
        appPayReqDTO.setPgRetUrl(pgRetUrl);
        appPayReqDTO.setAttach(attach);

        String url = Properties.URL.getString("GATEWAY_BASE_SCHEME") + Properties.URL.getString("APP_APY_URL");

        appPayReqDTO.setSignatureAlgorithm(KeyContants.SECURITY_TYPE);
        appPayReqDTO.setSignatureInfo(
                SecurityKit.signByRSA(HttpKit.httpGwParam2SignStr(appPayReqDTO.getAllParameters())));
        LOGGER.debug("开户请求参数：{},参与签名串：{}", appPayReqDTO,
                HttpKit.httpGwParam2SignStr(appPayReqDTO.getAllParameters()));
        LOGGER.debug("请求地址：{}", url);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("req/jspay_demo");
        modelAndView.addObject("url", url);
        modelAndView.addObject("payment", appPayReqDTO);

        return modelAndView;
    }
}
